package com.example.weibo3.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.weibo3.R;
import com.example.weibo3.bean.MessageItem;

import java.util.List;

public class CartAdapter extends ArrayAdapter<MessageItem> {
    public CartAdapter(@NonNull Context context, int resource, @NonNull List<MessageItem> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    // 返回每一行的显示内容
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 加载布局文件，生成一个view
        View view = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item, parent, false);

        // 获取数据，通过position获取数组中对应的一项数据
        MessageItem messageItem = getItem(position);

        // 图片渲染填充
        ImageView imageView = view.findViewById(R.id.image);
        Glide.with(getContext())
                .load(messageItem.getImage())
                .into(imageView);

        // 标题文本渲染
        TextView title = view.findViewById(R.id.title);
        title.setText(messageItem.getTitle());
        // 内容文本渲染
        TextView content = view.findViewById(R.id.content);
        content.setText(messageItem.getContent());
        // 时间文本渲染
        TextView time = view.findViewById(R.id.time);
        time.setText(messageItem.getTime());

        return view;
    }
}
